package com.etime.perfectschool.controller;

import com.etime.perfectschool.pojo.ActivitiesPhoto;
import com.etime.perfectschool.pojo.NewsPhoto;
import com.etime.perfectschool.service.ActivitiesPhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
@RestController
@CrossOrigin
public class ActivitiesPhotoController {
    @Autowired
    private ActivitiesPhotoService  activitiesPhotoService;
    //添加活动文件
    @PostMapping("/insertActivitiesPhoto")
    public int insertActivitiesPhoto(MultipartFile[] file, int activityId)
    {
        int i =0;
        String path = "D:\\perfectschool1\\src\\main\\resources\\static\\schoolnews";
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        try {
            for (MultipartFile multipartFile:file) {
                multipartFile.transferTo(new File(file1,multipartFile.getOriginalFilename()));
                String photoName = multipartFile.getOriginalFilename();
                ActivitiesPhoto activitiesPhoto=new ActivitiesPhoto(0,photoName,activityId);
                i = activitiesPhotoService.insertActivitiesPhoto(activitiesPhoto);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return i;
    }
    //删除指定活动图片
    @DeleteMapping("/deleteActivitiesPhoto")
    public boolean deleteActivitiesPhoto(int activityId)
    {
        return activitiesPhotoService.deleteActivitiesPhotoById(activityId);
    }
    //获取某条活动的全部图片
    @GetMapping("/getAllActivitiesPhotoByActivityId")
    public List<ActivitiesPhoto> getAllActivitiesPhotoByActivityId(int activityId) {
        return activitiesPhotoService.getAllActivitiesPhotoByactivityId(activityId);
    }
    //修改活动图片
    @PutMapping("/updateActivitiesPhoto")
    public boolean updateActivitiesPhoto(MultipartFile[] file,int activityPhotoId)
    {
        boolean i=false;
        String path = "D:\\pic";
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdirs();
        }
        try {
            for (MultipartFile multipartFile:file) {
                multipartFile.transferTo(new File(file1,multipartFile.getOriginalFilename()));
                String photoName = multipartFile.getOriginalFilename();
                ActivitiesPhoto activitiesPhoto=new ActivitiesPhoto(activityPhotoId,photoName,0);
                i = activitiesPhotoService.updateActivitiesPhoto(activitiesPhoto);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return i;
    }
}
